﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace JobbJakt
{
    public partial class Users
    {
        

        /// <summary>
        /// Konstruktor som tar in alla parametrar
        /// </summary>
        /// 
        public Users(string userName, string socialSecurityNumber, string passWord, Authorization authorization,
            string firstName, string lastName, string address, string zipCode, string city, string phone, string email,
            string interest, string attributes, string photo, System.DateTime registrationDate, bool active, bool driversLicense)
        {
            UserName = userName;
            SocialSecurityNumber = socialSecurityNumber;
            Password = passWord;
            Authorization = authorization;
            FirstName = firstName;
            LastName = lastName;
            Address = address;
            ZipCode = zipCode;
            City = city;
            Phone = phone;
            Email = email;
            Interest = interest;
            Attributes = attributes;
            Photo = photo;
            RegistrationDate = registrationDate;
            Active = active;
            DriversLicense = driversLicense;
        }

        /// <summary>
        /// Konstruktor som tar in alla parametrar utom Interest och Photo som är nullvärden
        /// </summary>
        public Users(string userName, string socialSecurityNumber, string passWord, Authorization authorization,
            string firstName, string lastName, string address, string zipCode, string city, string phone, string email,
            string attributes, System.DateTime registrationDate, bool active, bool driversLicense)
        {
            UserName = userName;
            SocialSecurityNumber = socialSecurityNumber;
            Password = passWord;
            Authorization = authorization;
            FirstName = firstName;
            LastName = lastName;
            Address = address;
            ZipCode = zipCode;
            City = city;
            Phone = phone;
            Email = email;
            Attributes = attributes;
            RegistrationDate = registrationDate;
            Active = active;
            DriversLicense = driversLicense;
        }

        /// <summary>
        /// Lägger till en användare
        /// </summary>
        /// <param name="user">Tar in en Användare av typen Users</param>
        /// <returns></returns>
        public static int AddUser(Users user)
        {
            var query = JobbJaktDB.Userses.Add(user);
            return JobbJaktDB.Save();
        }


        /// <summary>
        /// Redigera en användare
        /// </summary>
        /// <param name="user">Tar in en användare av typen Users</param>
        /// <returns></returns>
        public static int UpdateUser(Users user)
        {
           JobbJaktDB.Userses.AddOrUpdate(user);
           return JobbJaktDB.Save();
        }

        /// <summary>
        /// Tar bort en användare
        /// </summary>
        /// <param name="userName">userName för användaren som ska tas bort</param>
        public static int RemoveUser(string userName)
        {
            var query = JobbJaktDB.Userses
                .Where(u => u.UserName == userName)
                .Select(u => u.Active == false).First();
            return JobbJaktDB.Save();
        }

        /// <summary>
        /// Skriver ut en användare
        /// </summary>
        /// <param name="userName">Användarnamn på användaren du vill skriva ut</param>
        /// <returns>Användaren</returns>
        public static Users PrintUser(string userName)
        {
            var query = JobbJaktDB.Userses
                .SingleOrDefault(u => u.UserName == userName);
            return query;
        }

        /// <summary>
        /// Hämtar alla aktiva användare
        /// </summary>
        /// <returns>En lista av användare</returns>
        public static List<Users> PrintActiveUsers()
        {
            var query = JobbJaktDB.Userses
                .Where(u => u.Active == true).ToList();
            return query;
        }

        public static DateTime GetRegistrationDate(string userName )
        {
            return 
                (from f in JobbJaktDB.Userses
                where f.UserName == userName
                select f.RegistrationDate).SingleOrDefault();
        }

        /// <summary>
        /// Metod för att kontrollera om användaren finns registrerad
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public static bool ValidateLogin(string userName, string passWord)
        {
            var query =
                (from f in JobbJaktDB.Userses
                where f.UserName == userName && f.Password == passWord
                select f).Count();

            if (query > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static Users GetUserAuthorization(string userName, string passWord)
        {
            return 
                (from f in JobbJaktDB.Userses
                where f.UserName == userName && f.Password == passWord
                select f).SingleOrDefault();
        }

        public static int ChangeAuthorization(string userName, int authorization)
        {
            var query =
                (from f in JobbJaktDB.Userses
                where f.UserName == userName
                select f).SingleOrDefault();

            query.Authorization = (Authorization) authorization;
            return JobbJaktDB.Save();
        }

        public static bool UserExist(string userName)
        {
            var query =
                (from f in JobbJaktDB.Userses
                where f.UserName == userName
                select f).Count();
            if (query > 0)
            {
                return true;
            }
            return false;
        }
        public static Users TheUser(string userName)
        {
            var query =
                (from f in JobbJaktDB.Userses
                 where f.UserName == userName
                 select f).SingleOrDefault();
            return query;
        }

        /// <summary>
        /// Kollar om Interest och/eller Photo är null eller ej och returnerar därefter korrekt string.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (Interest == null)
            {
                return string.Format(
                        "Användarnamn: {0}\r\nPersonnr: {1}\r\nLösenord:{2}\r\nAutentisering: {3}\r\nFörnamn: {4}\r\nEfternamn: {5}\r\nAdress: {6}\r\nPostnr: {7}\r\nStad: {8}\r\n" +
                        "Telefon: {9}\r\nEmail: {10}\r\nEgenskaper: {11}\r\nFoto: {12}\r\nRegistreringsdatum: {13}\r\nAktiv: {14}\r\nKörkort: {15}",
                        UserName, SocialSecurityNumber, Password, Authorization, FirstName, LastName, Address, ZipCode,
                        City, Phone, Email, Attributes, Photo, RegistrationDate, Active, DriversLicense);
            }
            else if (Photo == null)
            {
                return string.Format(
                    "Användarnamn: {0}\r\nPersonnr: {1}\r\nLösenord:{2}\r\nAutentisering: {3}\r\nFörnamn: {4}\r\nEfternamn: {5}\r\nAdress: {6}\r\nPostnr: {7}\r\nStad: {8}\r\n" +
                    "Telefon: {9}\r\nEmail: {10}\r\nIntressen: {11}\r\nEgenskaper: {12}\r\nRegistreringsdatum: {13}\r\nAktiv: {14}\r\nKörkort: {15}",
                    UserName, SocialSecurityNumber, Password, Authorization, FirstName, LastName, Address, ZipCode,
                    City, Phone, Email, Interest, Attributes, Photo, RegistrationDate, Active, DriversLicense);

            }
            else if (Interest == null && Photo == null)
            {
                return string.Format(
                     "Användarnamn: {0}\r\nPersonnr: {1}\r\nLösenord:{2}\r\nAutentisering: {3}\r\nFörnamn: {4}\r\nEfternamn: {5}\r\nAdress: {6}\r\nPostnr: {7}\r\nStad: {8}\r\n" +
                    "Telefon: {9}\r\nEmail: {10}\r\nEgenskaper: {11}\r\nRegistreringsdatum: {12}\r\nAktiv: {13}\r\nKörkort: {14}",
                     UserName, SocialSecurityNumber, Password, Authorization, FirstName, LastName, Address, ZipCode,
                    City, Phone, Email, Interest, Attributes, Photo, RegistrationDate, Active, DriversLicense);
            }

            else
            {
                return string.Format(
                        "Användarnamn: {0}\r\nPersonnr: {1}\r\nLösenord:{2}\r\nAutentisering: {3}\r\nFörnamn: {4}\r\nEfternamn: {5}\r\nAdress: {6}\r\nPostnr: {7}\r\nStad: {8}\r\n" +
                        "Telefon: {9}\r\nEmail: {10}\r\nIntressen: {11}\r\nEgenskaper: {12}\r\nFoto: {13}\r\nRegistreringsdatum: {14}\r\nAktiv: {15}\r\nKörkort: {16}",
                        UserName, SocialSecurityNumber, Password, Authorization, FirstName, LastName, Address, ZipCode,
                        City, Phone, Email, Interest, Attributes, Photo, RegistrationDate, Active, DriversLicense);

            }
        }
    }
}

